/// <reference path="../Framework/BrowserApi.js" />
/// <reference path="../Framework/Toolbar/Toolbar.css" />
/// <reference path="Content.js" />
/// <reference path="Global.js" />
/// <reference path="MouseAndKeyboardEvents.js" />
/// <reference path="SelectionProcessing.js" />
/// <reference path="Spreadsheet.js" />
/// <reference path="ToolbarItems.js" />

var VirtualScrolling = new function ()
{
    // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

    this.Initialize = function ()
    {
        ScrollPanel.onscroll = this.OnScrollEventHandler
    }

    // Point to all table cells and all span elements in use.
    var TextElementsInUse = new Array();
    var CellElementsInUse = new Array();

    // When a scroll happens, empty the spreadsheet sells, set them to their default style, and fill the spreadsheet with the new 
    // information that corresponds to the scroll location.
    this.OnScrollEventHandler = function ()
    {
        SelectionProcessing.SelectingCells = false;

        // Reset the span elements in use.
        for (var elementIndex = 1; elementIndex < TextElementsInUse.length; elementIndex++)
        {
            var textElement = TextElementsInUse[elementIndex];
            textElement.textContent = ""
        }

        // Reset the table cells in use.
        for (var elementIndex = 1; elementIndex < CellElementsInUse.length; elementIndex++)
        {
            var cellElement = CellElementsInUse[elementIndex];
            cellElement.className = "NormalCellStyle"
        }

        var textElementIndex = 1
        var cellElementIndex = 1
        TextElementsInUse = new Array();
        CellElementsInUse = new Array();

        // Find the beginning of the virtual area to display
        var virtualColumnNumber = 1;
        var virtualRowNumber = 1;
        if (ScrollPanel.scrollLeft != 0)
        {
            virtualColumnNumber = Math.round(ScrollPanel.scrollLeft / Spreadsheet.StandardColumnWidth)
            if (virtualColumnNumber == 0)
            {
                virtualColumnNumber = 1
            }
        }
        if (ScrollPanel.scrollTop != 0)
        {
            virtualRowNumber = Math.round(ScrollPanel.scrollTop / Spreadsheet.StandardRowHeight)
            if (virtualRowNumber == 0)
            {
                virtualRowNumber = 1
            }
        }

        // Write the new row numbers.
        var virtualRowNumberCount = virtualRowNumber
        for (var rowId = 1; rowId < SpreadsheetBody.rows.length; rowId++)
        {
            var cellText = document.getElementById("Text_" + rowId + "_0");
            cellText.textContent = virtualRowNumberCount
            virtualRowNumberCount++

            TextElementsInUse[textElementIndex] = cellText
            textElementIndex++
        }

        // Write the new column numbers.
        var virtualColumnNumberCount = virtualColumnNumber
        for (var columnId = 1; columnId < SpreadsheetBody.rows[0].cells.length; columnId++)
        {
            var cellText = document.getElementById("Text_0_" + columnId);
            cellText.textContent = virtualColumnNumberCount
            virtualColumnNumberCount++

            TextElementsInUse[textElementIndex] = cellText
            textElementIndex++
        }

        var virtualColumnNumberStart = virtualColumnNumber
        for (var rowId = 1; rowId < SpreadsheetBody.rows.length; rowId++)
        {
            // Extract a virtual row, if it does not exist, continue to the next row
            var dataRow = SpreadsheetContent.Rows[virtualRowNumber];
            if (!dataRow)
            {
                virtualRowNumber++
                continue
            }

            var spreadsheetRowCells = SpreadsheetBody.rows[rowId].cells;
            virtualColumnNumber = virtualColumnNumberStart
            for (var columnId = 1; columnId < spreadsheetRowCells.length; columnId++)
            {
                // Extract a virtual cell, if it does not exist, continue to the next cell
                var dataCell = dataRow.Cells[virtualColumnNumber]
                if (!dataCell)
                {
                    virtualColumnNumber++
                    continue
                }

                // Set the cell text, decoration, etc.
                var cellText = document.getElementById("Text_" + rowId + "_" + columnId);
                cellText.textContent = dataCell.Text
                cellText.className = dataCell.Formatting               
                TextElementsInUse[textElementIndex] = cellText
                textElementIndex++

                var spreadsheetCell = spreadsheetRowCells[columnId];
                CellElementsInUse[cellElementIndex] = spreadsheetCell
                cellElementIndex++
                virtualColumnNumber += dataCell.ColumnSpan

                // If the text is going over one column, update the underplaying cell styles.
                if (dataCell.ColumnSpan > 1)
                {
                    for (var spanIndex = columnId + 1; spanIndex < columnId + dataCell.ColumnSpan; spanIndex++)
                    {
                        spreadsheetCell = spreadsheetRowCells[spanIndex];
                        spreadsheetCell.className = "ColumnSpanCellStyle"
                        CellElementsInUse[cellElementIndex] = spreadsheetCell
                        cellElementIndex++
                    }
                }
                columnId += dataCell.ColumnSpan - 1
            }

            virtualRowNumber++
        }
    }
}